package pl.edu.mimuw.irs.rcp.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.gef.ui.actions.UpdateAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;

import pl.edu.mimuw.irs.rcp.view.navigation.NavigationViewContentProvider;
import pl.edu.mimuw.irs.rcp.view.navigation.NavigationViewLabelProvider;

public class NavigationView extends ViewPart {
	public static final String ID = "pl.edu.mimuw.irs.view.navigation";
	
	// *** Attributes

	private TreeViewer viewer;
	private ActionRegistry actionRegistry;
	private List<String> selectionActionIDs = new ArrayList<String>();
	private ISelectionListener selectionListener = new ISelectionListener() {
		public void selectionChanged(IWorkbenchPart part, ISelection selection) {
			updateActions(selectionActionIDs);
		}
	};
	private IResourceChangeListener resourceChangeListener = new IResourceChangeListener() {
		@Override
		public void resourceChanged(IResourceChangeEvent event) {
			updateActions(selectionActionIDs);
			viewer.refresh();
		}
	};
	// Getters and setters
	public ActionRegistry getActionRegistry() {
		if (actionRegistry == null) actionRegistry = new ActionRegistry();
		return actionRegistry;
	}
	protected void addSelectionAction(SelectionAction action) {
		getActionRegistry().registerAction(action);
		selectionActionIDs.add(action.getId());
	}
	public ISelectionListener getSelectionListener() {
		return selectionListener;
	}
	protected void addAction(IAction action) {
		getActionRegistry().registerAction(action);
	}
	
	// *** Methods

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		
		// Creating actions
		createActions();
		// Creating selection listener
		getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(
				getSelectionListener());
		
		ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceChangeListener);
		
	}
	
	@Override
	public void dispose() {
		super.dispose();

		// Disposing actions
		getActionRegistry().dispose();
		// Disposing selection listener
		getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(
				getSelectionListener());
	}
	
	private void createActions() {
		//addSelectionAction(new DeleteAction((IWorkbenchPart) this));
	}

	@Override
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		
		viewer.setContentProvider(new NavigationViewContentProvider());
		viewer.setLabelProvider(new NavigationViewLabelProvider());
		IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot();
		viewer.setInput(rootWorkspace);
		getSite().setSelectionProvider(viewer);
		
		hookDoubleClickCommand();
	}

	private void hookDoubleClickCommand() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IHandlerService handlerService = (IHandlerService) getSite()
						.getService(IHandlerService.class);
				
				if (event.getSelection() instanceof StructuredSelection) {
					StructuredSelection structuredSelection = (StructuredSelection)event.getSelection();
					
					if (structuredSelection.getFirstElement() instanceof IResource) {
						IResource resource = (IResource) structuredSelection.getFirstElement();
						
						if ("irs".equalsIgnoreCase(resource.getFileExtension())) {
							Event widgetEvent = new Event();

							widgetEvent.data = resource;
							
							try {
								handlerService.executeCommand(
										"pl.edu.mimuw.irs.command.restoreirsworkflow", widgetEvent);
							} catch (Exception ex) {
								throw new RuntimeException(
										"pl.edu.mimuw.irs.command.restoreirsworkflow not found");
							}
						}
					}
				}
			}
		});
	}
	
	@Override
	public void setFocus() {
		
	}
	
	private void updateActions(List<String> actionIDs) {
		for (String id : actionIDs) {
			IAction action = getActionRegistry().getAction(id);
			if (action != null && action instanceof UpdateAction)
				((UpdateAction) action).update();
		}
	}

}
